import requests  # 数据请求模块
import re  # 正则表达式模块
import json  # 数据类型处理模块
from tqdm import tqdm  # 进度条配置
import os  # 处理文件和目录

headers = {
    'cookie': 'VISITOR_INFO1_LIVE=9qZVrzB27uI; PREF=f4=4000000&tz=Asia.Shanghai; _ga=GA1.2.621834420.1648121145; _gcl_au=1.1.1853038046.1648121145; NID=511=Zc1APdmEbCD-iqVNVgI_vD_0S3LVI3XSfl-wUZEvvMU2MLePFKsQCaKUlUtchHSg-kWEVMGOhWUbxpQMwHeIuLjhxaslwniMh1OsjVfmOeTfhpwcRYpMgqpZtNQ7qQApY21xEObCvIez6DCMbjRhRQ5P7siOD3X87QX0CFyUxmY; OTZ=6430350_24_24__24_; GPS=1; YSC=0E115KqM_-I; GOOGLE_ABUSE_EXEMPTION=ID=d02004902c3d0f4d:TM=1648620854:C=r:IP=47.57.243.77-:S=YmZXPW7dxbu83bDuauEpXpE; CONSISTENCY=AGDxDeNysJ2boEmzRP4v6cwgg4NsdN4-FYQKHCGhA0AeW1QjFIU1Ejq1j8l6lwAc6c-pYTJiSaQItZ1M6QeI1pQ3wictnWXTOZ6_y8EKlt0Y_JdakwW6srR39-NLuPgSgXrXwtS0XTUGXpdnt4k3JjQ',
    'referer': 'https://www.youtube.com/results?search_query=jk%E7%BE%8E%E5%A5%B3',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36'
}
url = 'https://www.youtube.com/watch?v=ImoXcSpR_io'
response = requests.get(url=url, headers=headers)
print(response.text)
json_str = re.findall('var ytInitialPlayerResponse = (.*?);var', response.text)[0]
json_data = json.loads(json_str)
# 获取视频链接
video_url = json_data['streamingData']['adaptiveFormats'][0]['url']
# 获取音频链接
audio_url = json_data['streamingData']['adaptiveFormats'][-2]['url']
# 获取标题
title = json_data['videoDetails']['title']
title = title.replace(' ', '')
title = re.sub(r'[\/:|?*"<>]', '', title)
print(video_url)
print(audio_url)
print(title)
# 获取视频
video = requests.get(video_url, stream=True)
# 获取视频大小
file_size = int(video.headers.get('Content-Length'))
# 初始化进度条
video_pbar = tqdm(total=file_size)
# 保存视频
with open(f'{title}.mp4', mode='wb') as f:
    # 把视频分成 1024 * 1024 * 2 为等分的大小 进行遍历
    for video_chunk in video.iter_content(1024 * 1024 * 2):
        # 写入数据
        f.write(video_chunk)
        # 更新进度条
        video_pbar.set_description(f'正在下载{title}视频中......')
        # 更新进度条长度
        video_pbar.update(1024 * 1024 * 2)
    # 下载完毕
    video_pbar.set_description('下载完成！')
    # 关闭进度条
    video_pbar.close()

# 获取音频
audio = requests.get(audio_url, stream=True)
file_size = int(audio.headers.get('Content-Length'))
audio_pbar = tqdm(total=file_size)
# 保存音频
with open(f'{title}.mp3', mode='wb') as f:
    for audio_chunk in audio.iter_content(1024 * 1024 * 2):
        f.write(audio_chunk)
        audio_pbar.set_description(f'正在下载{title}音频中......')
        audio_pbar.update(1024 * 1024 * 2)
    audio_pbar.set_description('下载完成！')
    audio_pbar.close()


def merge(title):
    ffmpeg = r'E:\ffmpeg\bin\ffmpeg.exe -i ' + title + '.mp4 -i ' + title + '.mp3 -acodec copy -vcodec copy ' + title + '-out.mp4'
    os.popen(ffmpeg)


# 合并音视频
merge(title)
